// 方法1
// type Flatten<T extends any[], U extends any[] = []> = T extends [
//   infer F,
//   ...infer Rest
// ]
//   ? F extends never
//     ? U
//     : F extends any[]
//     ? [...U, ...Flatten<F>, ...Flatten<Rest>]
//     : [...U, F, ...Flatten<Rest>]
//   : U;

// 方法2
type Flatten<T extends any[], U extends any[] = []> = T extends [
  infer F,
  ...infer Rest
]
  ? F extends any[]
    ? [...U, ...Flatten<F>, ...Flatten<Rest>]
    : [...U, F, ...Flatten<Rest>]
  : U;

type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]>;
